package com.lzy.business.facade;

import com.github.pagehelper.PageInfo;
import com.lzy.business.response.AgentBillScreenListResponse;
import com.lzy.business.response.AgentBilltStatisticsInfoResponse;
import com.lzy.framework.core.datasource.multiple.DataSource;
import com.lzy.framework.core.datasource.multiple.DataSourceHolder;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.member.apis.MemberUserServiceApi;
import com.lzy.member.dtos.MemberUserDto;
import com.lzy.product.apis.AgentCommissionBillServiceApi;
import com.lzy.product.dtos.agent.AgentBillScreenListDto;
import com.lzy.product.dtos.agent.AgentCommissionBillDto;
import com.lzy.product.dtos.agent.AgentCommissionBilltStatisticsDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class AgentCommissionBillFacade {

    @Autowired
    private AgentCommissionBillServiceApi agentCommissionBillServiceApi;

    @Autowired
    private MemberUserServiceApi memberUserServiceApi;

    public PageInfo<AgentCommissionBillDto> getAgentCommissionBillList(Pager pager, AgentCommissionBillDto dto) {
        PageInfo pageInfo = agentCommissionBillServiceApi.selectList(pager, dto);
        List<AgentCommissionBillDto> agentCommissionBillDtoList = pageInfo.getList();
        if (null != agentCommissionBillDtoList && !agentCommissionBillDtoList.isEmpty() && agentCommissionBillDtoList.size() > 0) {
            for (AgentCommissionBillDto agentCommissionBillDto : agentCommissionBillDtoList) {
                if(null != agentCommissionBillDto.getMemberId()){
                    DataSourceHolder.setDataSource(DataSource.DATASOURCE_COMMON);
                    MemberUserDto memberUserDto = memberUserServiceApi.findById(agentCommissionBillDto.getMemberId());
                    DataSourceHolder.clearDataSource();
                    agentCommissionBillDto.setMemberName(memberUserDto.getName());
                }
            }
        }

        return pageInfo;
    }

    public AgentBilltStatisticsInfoResponse getStatistics() {
        AgentBilltStatisticsInfoResponse agentBilltStatisticsInfoResponse = new AgentBilltStatisticsInfoResponse();
        //佣金合计
        AgentCommissionBilltStatisticsDto agentCommissionBilltStatisticsDto = agentCommissionBillServiceApi.statisticsBill();
        //按天佣金合计
        List<AgentCommissionBilltStatisticsDto> agentCommissionBilltStatisticsDtoList = agentCommissionBillServiceApi.statisticsBillForMonth();
        //7天时间空值处理
        List<AgentCommissionBilltStatisticsDto> agentCommissionBilltStatisticsDtoList1 = new ArrayList<>();
        List<AgentCommissionBilltStatisticsDto> dateList = getDateBefore(7);
        for(AgentCommissionBilltStatisticsDto dates : dateList){
            if(null != agentCommissionBilltStatisticsDtoList && agentCommissionBilltStatisticsDtoList.size() > 0){
                for(int i = 0 ;i < agentCommissionBilltStatisticsDtoList.size() ; i++){
                    if(dates.getBillDay().equals(agentCommissionBilltStatisticsDtoList.get(i).getBillDay())){
                        agentCommissionBilltStatisticsDtoList1.add(agentCommissionBilltStatisticsDtoList.get(i));
                        break;
                    }else if(i == agentCommissionBilltStatisticsDtoList.size()-1){
                        AgentCommissionBilltStatisticsDto agentCommissionBilltStatisticsDto1 = new AgentCommissionBilltStatisticsDto();
                        agentCommissionBilltStatisticsDto1.setBillCount(0L);
                        agentCommissionBilltStatisticsDto1.setBillSum(new BigDecimal(0));
                        agentCommissionBilltStatisticsDto1.setBillMonth(dates.getBillMonth());
                        agentCommissionBilltStatisticsDto1.setBillDay(dates.getBillDay());
                        agentCommissionBilltStatisticsDtoList1.add(agentCommissionBilltStatisticsDto1);
                    }
                }
            }else{
                AgentCommissionBilltStatisticsDto agentCommissionBilltStatisticsDto1 = new AgentCommissionBilltStatisticsDto();
                agentCommissionBilltStatisticsDto1.setBillCount(0L);
                agentCommissionBilltStatisticsDto1.setBillSum(new BigDecimal(0));
                agentCommissionBilltStatisticsDto1.setBillMonth(dates.getBillMonth());
                agentCommissionBilltStatisticsDto1.setBillDay(dates.getBillDay());
                agentCommissionBilltStatisticsDtoList1.add(agentCommissionBilltStatisticsDto1);
            }
        }
        //统计昨天和今天分拥
        List<AgentCommissionBilltStatisticsDto> agentCommissionBilltStatisticsDtos = agentCommissionBillServiceApi.statisticsBillForDay();

        agentBilltStatisticsInfoResponse.setTotalBill(agentCommissionBilltStatisticsDto);
        agentBilltStatisticsInfoResponse.setMonthBill(agentCommissionBilltStatisticsDtoList1);
        agentBilltStatisticsInfoResponse.setDayBill(agentCommissionBilltStatisticsDtos);
        return agentBilltStatisticsInfoResponse;
    }

    public List<AgentBillScreenListDto> getAgentBillScreenList(Integer type) {
        List<AgentBillScreenListDto> agentBillScreenListDtoList = agentCommissionBillServiceApi.selectScreenList(type);
        return agentBillScreenListDtoList;
    }

    public List<AgentBillScreenListDto> getSystemList(Long sourceId,Integer type) {
        List<AgentBillScreenListDto> agentBillScreenListDtoList = agentCommissionBillServiceApi.selectSystemList(sourceId,type);
        return agentBillScreenListDtoList;
    }

    public List<AgentCommissionBilltStatisticsDto> getDateBefore(int day){
        List<AgentCommissionBilltStatisticsDto> dateList = new ArrayList<>();
        Calendar now =Calendar.getInstance();
        for(int i = 0; i < day ;i++){
            now.setTime(new Date());
            now.set(Calendar.DATE,now.get(Calendar.DATE)-i);
            AgentCommissionBilltStatisticsDto agentCommissionBilltStatisticsDto = new AgentCommissionBilltStatisticsDto();
            agentCommissionBilltStatisticsDto.setBillDay(now.get(Calendar.DAY_OF_MONTH));
            agentCommissionBilltStatisticsDto.setBillMonth(now.get(Calendar.MONTH) + 1);
            dateList.add(agentCommissionBilltStatisticsDto);
        }
        return dateList;
    }


}
